#include <stdio.h>
#include <assert.h>
#include <TabLite.h>

#define _(str) #str 

void example()
{
    TabLite* rootTab = TabLiteNew();
    assert(rootTab);

    TabLite* netTab = TabLiteNew();
    assert(netTab);
    
    TabLite* pwdTab = TabLiteNew();
    assert(pwdTab);
    
    TabLite* sysTab = TabLiteNew();
    assert(sysTab);
    
    TabElem te;
    assert(TabLiteSet(rootTab, "netconfig", TabElemSetTab(&te, netTab)));
    assert(TabLiteSet(rootTab, "pwdconfig", TabElemSetTab(&te, pwdTab)));
    assert(TabLiteSet(rootTab, "sysconfig", TabElemSetTab(&te, sysTab)));
    
    /* net config */
    assert(TabLiteSet(netTab, "ip", TabElemSet(&te, "192.168.0.10")));
    assert(TabLiteSet(netTab, "gateway", TabElemSet(&te, "192.168.0.1")));
    assert(TabLiteSet(netTab, "netmask", TabElemSet(&te, "255.255.0.0")));
    assert(TabLiteSet(netTab, "port", TabElemSetInt(&te, 8080)));
    
    /* pwd config */
    assert(TabLiteSet(pwdTab, "root", TabElemSet(&te, "1234")));
    assert(TabLiteSet(pwdTab, "jasonhu", TabElemSet(&te, "tablite")));

    assert(TabLiteRemove(pwdTab, "jasonhu"));
    
    /* sys config */
    assert(TabLiteSet(sysTab, "PAGE_SIZE", TabElemSetInt(&te, 4096)));
    assert(TabLiteSet(sysTab, "CLCK", TabElemSetInt(&te, 100)));

    assert(TabLiteRemove(sysTab, "CLCK"));

    printf("ip:%s\n", TabElemGet(TabLiteGet(TabElemGetTab(TabLiteGet(rootTab, "netconfig")), "ip")));
    printf("gateway:%s\n", TabElemGet(TabLiteGet(TabElemGetTab(TabLiteGet(rootTab, "netconfig")), "gateway")));
    printf("netmask:%s\n", TabElemGet(TabLiteGet(TabElemGetTab(TabLiteGet(rootTab, "netconfig")), "netmask")));
    printf("port:%d\n", TabElemGetInt(TabLiteGet(TabElemGetTab(TabLiteGet(rootTab, "netconfig")), "port")));
    
    printf("pwd:%s\n", TabElemGet(TabLiteGet(TabElemGetTab(TabLiteGet(rootTab, "pwdconfig")), "root")));
    printf("pwd:%s\n", TabElemGet(TabLiteGet(TabElemGetTab(TabLiteGet(rootTab, "pwdconfig")), "jasonhu")));
    
    printf("PAGE_SIZE:%d\n", TabElemGetInt(TabLiteGet(TabElemGetTab(TabLiteGet(rootTab, "sysconfig")), "PAGE_SIZE")));
    printf("CLCK:%d\n", TabElemGetInt(TabLiteGet(TabElemGetTab(TabLiteGet(rootTab, "sysconfig")), "CLCK")));
    
    /* walk tab */
    TabElem* elem = TabLiteGetByNext(netTab, NULL);
    while (elem != NULL)
    {
        if (TabElemGetTag(elem) == TAB_TAG_INT)
            printf("elem:%d\n", TabElemGetInt(elem));
        else
            printf("elem:%s\n", TabElemGet(elem));
            
        elem = TabLiteGetByNext(netTab, elem);
    }

    TabLiteDel(rootTab);
}

void example2()
{
    TabLite* rootTab = TabLiteNew();
    assert(rootTab);

    TabLite* subTab = TabLiteNew();
    assert(subTab);

    TabElem te;
    assert(TabLiteSet(subTab, "name", TabElemSet(&te, "subtable")));
    
    printf("name:%s\n", TabElemGet(TabLiteGet(rootTab, "name")));
    printf("Attach:%s\n", TabLiteAttach(rootTab, subTab) == 0 ? "ok":"bad");
    printf("name:%s\n", TabElemGet(TabLiteGet(rootTab, "name")));
    printf("Dettach:%s\n", TabLiteDetach(rootTab, subTab) == 0 ? "ok":"bad");
    printf("name:%s\n", TabElemGet(TabLiteGet(rootTab, "name")));

    assert(TabLiteSet(rootTab, "name", TabElemSet(&te, "roottable")));
    
    printf("name:%s\n", TabElemGet(TabLiteGet(rootTab, "name")));
    printf("Attach:%s\n", TabLiteAttach(rootTab, subTab) == 0 ? "ok":"bad");
    printf("error:%s\n", TabErrorGet());
    printf("name:%s\n", TabElemGet(TabLiteGet(rootTab, "name")));
    printf("Dettach:%s\n", TabLiteDetach(rootTab, subTab) == 0 ? "ok":"bad");
    printf("error:%s\n", TabErrorGet());
    printf("name:%s\n", TabElemGet(TabLiteGet(rootTab, "name")));

    printf("error:%s\n", TabErrorGet());

    printf("name:%s\n", TabElemGet(TabLiteGet(rootTab, "test")));
    printf("error:%s\n", TabErrorGet());

    TabLiteDel(subTab);
    TabLiteDel(rootTab);
}

int main(int argc, char *argv[])
{
    example();
    example2();
    return 0;
}
